var hotelPreference = {
    perference : new Array(),
    data : JSON.parse(localStorage.getItem('hotel_'+ BT.context('hotelId') +'_order')),
    init : function(){
        var self = this;
        self.selectEvent();
        if(self.data && self.data.customer_preferences != ''){
            var arr = self.data.customer_preferences.split(',');
            self.perference = arr;
            $.each($('li'), function(i, k){
                var val = $(k).data('id');
                arr.filter(function(v){
                    if(v == val){
                        $(k).addClass('push');
                    }
                });
            });
        }
    },
    selectEvent : function(){
        var self = this;
        $('li').on('click', function(){
            var $this = $(this), n = 1;; 
            if($this.hasClass('only')) n = 0;
            if($this.hasClass('push')){
                $this.removeClass('push');
                if(n == 0){
                    self.perference.shift();
                }else{
                    self.perference.pop();
                }
            }else{
                $this.addClass('push');
                if(n == 0) $this.siblings('.only').removeClass('push');
                self.perference[n] = $this.data('id');
            }
        });
    }
};
$(function(){
    hotelPreference.init();
    confirmEvent = function(){
        if(hotelPreference.perference){
            if(BT.context('hotelId')){
                if( ! hotelPreference.perference[0] )hotelPreference.perference.shift();       
                hotelPreference.data.customer_preferences = hotelPreference.perference.join(',');
                localStorage['hotel_'+ BT.context('hotelId') +'_order'] = JSON.stringify(hotelPreference.data);
            }
            location.href = location.origin + BT.context('backUrl') + location.search;
        } 
    }
    if($('body').data('version') == 'None'){
        var $btn = $('.finish-btn');
        $btn.removeClass('hide');
        $btn.on('click', function(){
            $(this).addClass('push');
            confirmEvent();
            setTimeout(function(){
                $(this).removeClass('push');
            }, 300);
        });
    }
});